clear all; close all; clc;

%internal parameters
C = 33*10e-12; %capasity
C1 = C/2; %recalculated capasity
Rd=40000; %diod's parameters
rd=1.5;
U0=0.6;
tay=rd*C1;

%parameters of input signal
ff=1/tay/8; %frequency
Period = 1/ff;

%time scale
T=1/1000/ff;
t=0:T:(5*Period);
lt=length(t);

%% input signal #1 - Step


%description of signal
E=10*ones(1,lt);
figure (1);
plot (t*1e9, E);
xlabel('t, nsec');
ylabel('Amplitude, V');

%memory allotment
Uc=nan(1, lt);
U=nan(1,lt);

%initial state
Uc(1) = 0;

%circuit's model
EE=abs(E);
figure (2);
plot (t*1e9, [E;EE]);
xlabel('t, nsec');
ylabel('Amplitude, V');

for k = 2:lt
    
        Uc(k) = (rd*C1*Uc(k-1) + EE(k)*T) / (rd*C1 + T);
    
end

U=Uc/2;

figure (3); 
plot (t*1e9,[Uc;U;EE]);
legend('Uc','U', 'EE')
xlabel('t, nsec');
ylabel('Amplitude, V');

%% input signal #2 - AWGN

%memory allotment
Uc=nan(1, lt);
U=nan(1, lt);

%description of signal
E=10*randn(1,lt);

figure (4);
plot (t*1e9, E);
xlabel('t, nsec');
ylabel('Amplitude, V');

%initial state
Uc(1) = 0;

%circuit's model
EE=abs(E);

figure (5);
plot (t*1e9, [E;EE]);
xlabel('t, nsec');
ylabel('Amplitude, V');

for k = 2:lt
    
        Uc(k) = (rd*C1*Uc(k-1) + EE(k)*T) / (rd*C1 + T);
    
end

U=Uc/2;

figure (6); 
plot (t*1e9,[EE;Uc;U]);
legend('Uc','U', 'EE')
xlabel('t, nsec');
ylabel('Amplitude, V');

%% input signal #3 - Sin

%memory allotment
Uc=nan(4, lt);
U=nan(4, lt);

%initial state
Uc(:,1) = 0;

A = [0,1,2,3];
for n=1:length(A)
    
    %description of signal
    E(n,:)=A(n)*sin(2*pi*ff*t);
        
    %circuit's model
    EE(n,:)=abs(E(n,:));
    
    figure (7);
    subplot(2,2,n);
    plot (t*1e9, [E(n,:);EE(n,:)]);
    xlabel('t, nsec');
    ylabel('Amplitude, V');
    legend('Input', 'Abs_Input')
    
    %output signal
    for k = 2:lt
        
        Uc(n,k) = (rd*C1*Uc(n,k-1) + EE(n,k)*T) / (rd*C1 + T);
    
    end

    U(n,:)=Uc(n,:)/2;
        
    figure (8); 
    subplot (2,2,n);
    plot (t*1e9,[EE(n,:);Uc(n,:);U(n,:)]);
    legend( 'Abs_Input','Uc','U')
    xlabel('t, nsec');
    ylabel('Amplitude, V');
end

